package org.microblog.service.impl;

import org.apache.ibatis.session.SqlSession;
import org.microblog.dao.FollowMapper;
import org.microblog.entity.Follow;
import org.microblog.entity.User;
import org.microblog.service.FollowService;
import org.microblog.util.DatabaseUtil;
import org.microblog.util.MyBatisUtil;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class FollowServiceImpl implements FollowService {
    SqlSession sqlSession = MyBatisUtil.createSqlSession();
    FollowMapper followMapper = sqlSession.getMapper(FollowMapper.class);
    @Override
    public int getFollowedCount(int uId) throws SQLException {
        Connection conn = null;
        try{
            return followMapper.getFollowedCount(uId);
        }catch(SQLException e){
            e.printStackTrace();
            throw e;
        }finally{
            MyBatisUtil.closeSqlSession(sqlSession);
        }
    }

    @Override
    public int getFansCount(int uId) throws SQLException {
        Connection conn = null;
        try{
            return followMapper.getFansCount(uId);
        }catch(SQLException e){
            e.printStackTrace();
            throw e;
        }finally{
            MyBatisUtil.closeSqlSession(sqlSession);
        }
    }

    @Override
    public List<User> getFollowedByUidAndKeyword(int uId, String keyword) throws SQLException {
        Connection conn = null;
        try{
            return followMapper.getFollowedByUidAndKeyword(uId,keyword);
        }catch(SQLException e){
            e.printStackTrace();
            throw e;
        }finally{
            MyBatisUtil.closeSqlSession(sqlSession);
        }
    }

    @Override
    public List<User> getFansByUidAndKeyword(int uId, String keyword) throws SQLException {
        Connection conn = null;
        try{
            return followMapper.getFansByUidAndKeyword(uId, keyword);
        }catch(SQLException e){
            e.printStackTrace();
            throw e;
        }finally{
            MyBatisUtil.closeSqlSession(sqlSession);
        }
    }

    @Override
    public boolean isFollowed(int uId, int followedUid) throws SQLException {
        Connection conn = null;
        try{
            return followMapper.isFollowed(uId,followedUid);
        }catch(SQLException e){
            e.printStackTrace();
            throw e;
        }finally{
            MyBatisUtil.closeSqlSession(sqlSession);
        }
    }

    @Override
    public int addFollower(Follow follow) throws SQLException {
        Connection conn = null;
        int result =0;
        try{
            if(!followMapper.isFollowed(follow.getuId(),follow.getFollowerId())){
                result = followMapper.addFollower(follow);
            }else {
                result = -1;
            }
        }catch(SQLException e){
            e.printStackTrace();
            throw e;
        }finally{
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        return result;
    }

    @Override
    public int deleteFollower(Follow follow) throws SQLException {
        Connection conn = null;
        int result =0;
        try{
            if(followMapper.isFollowed(follow.getuId(),follow.getFollowerId())){
                result = followMapper.deleteFollower(follow);
            }
            sqlSession.commit();
        }catch(SQLException e){
            e.printStackTrace();
            if (conn != null)
                try {
                    conn.rollback();
                } catch (SQLException e1) {
                    e1.printStackTrace();
                }
            throw e;
        }finally{
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        return result;
    }
}
